package com.campus.client.controller;

import com.campus.client.net.ClientConnector;
import com.campus.client.view.StudentRegisterFrame;
import com.campus.common.message.StudentRegisterRequest;
import com.campus.common.message.StudentRegisterResponse;
import com.campus.common.vo.AcademicRecord;
import com.campus.common.vo.Student;

import javax.swing.*;
import java.awt.*;
import java.io.IOException;

public class StudentController extends Component {
    private StudentRegisterFrame studentRegisterFrame;
    private ClientConnector connector;

    public StudentController(StudentRegisterFrame studentRegisterFrame, ClientConnector connector) {
        this.studentRegisterFrame = studentRegisterFrame;
        this.connector = connector;
        setupEventHandlers();
    }

    private void setupEventHandlers() {
        // 设置登录按钮事件
        studentRegisterFrame.setRegisterButtonAction(event -> {
            try {
                registerControl();
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });

        studentRegisterFrame.setBackButtonAction(event -> {

            try {
                studentRegisterFrame.showLoginFrame();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        });

    }

    private String registerControl() throws ClassNotFoundException, IOException {

        if (studentRegisterFrame.getStudentId().isEmpty()||studentRegisterFrame.getGender().isEmpty()||studentRegisterFrame.getAge().isEmpty()
                ||studentRegisterFrame.getGrade().isEmpty()||studentRegisterFrame.getMajor().isEmpty()||studentRegisterFrame.getClassName().isEmpty()
                ||studentRegisterFrame.getEnrollmentDate().isEmpty()||studentRegisterFrame.getContact().isEmpty()||studentRegisterFrame.getEnrollmentDate().isEmpty()) {
            JOptionPane.showMessageDialog(this,
                    "个人信息不能为空！",
                    "输入错误",
                    JOptionPane.ERROR_MESSAGE);
            return "None";
        }

        AcademicRecord academicRecord = new AcademicRecord(studentRegisterFrame.getRecordId(),studentRegisterFrame.getGender(),
                Integer.valueOf(studentRegisterFrame.getAge()),studentRegisterFrame.getGrade(),studentRegisterFrame.getMajor(),
                studentRegisterFrame.getClassName(),"在读",studentRegisterFrame.getEnrollmentDate(),studentRegisterFrame.getContact());
        Student student = new Student(studentRegisterFrame.getStudentId(),studentRegisterFrame.getName(),academicRecord);



        StudentRegisterResponse resp = connector.register(studentRegisterFrame.getStudentId(),studentRegisterFrame.getPassword(),student);

        if(resp.isSuccess()){
            studentRegisterFrame.showLoginFrame();

            JOptionPane.showMessageDialog(this,
                    "注册成功！\n欢迎您的到来，" + student.getStudentId() + "！",
                    "注册成功",
                    JOptionPane.INFORMATION_MESSAGE);
        }
        else{
            JOptionPane.showMessageDialog(this,
                    "该用户名已注册！",
                    "输入错误",
                    JOptionPane.ERROR_MESSAGE);
        }

        return resp.getMessage();


    }
}
